package com.yinhe.music.yhmusic.main.home;

import com.apkfuns.logutils.LogUtils;
import com.yinhe.music.common.utils.JsonUtil;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.cache.CacheManager;
import com.yinhe.music.yhmusic.cache.HomeData;
import com.yinhe.music.yhmusic.model.HomeInfo;
import com.yinhe.music.yhmusic.network.response.ResponseTransformer;

import static com.yinhe.music.yhmusic.main.home.HomePageMusicFragment.HOME;

/**
 * HomePageFragment的P层
 *
 * @author 何晓华
 */
public class HomePresenter extends BasePresenter<BaseModel, HomeContract.IHomeView> implements HomeContract.IHomePresenter {

    @Override
    public void getRecommendBanners() {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getRecommendBanners()
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(this::parseHomeData, throwable -> {
                    getView().showMessage(throwable);
                    handleLocalHomeData();
                }));
    }

    @Override
    public void getRecommendSongs(int curPage, int pageSize) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getRecommendSongs(curPage, pageSize)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(homeInfo -> getView().setRecommend(homeInfo), throwable -> {
                    getView().showMessage(throwable);
                    handleLocalHomeData();
                }));
    }

    @Override
    public void getRecommendNewSongs(int curPage, int pageSize) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getRecommendNewSongs(curPage, pageSize)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(homeInfo -> getView().setRecommend(homeInfo), throwable -> {
                    getView().showMessage(throwable);
                    handleLocalHomeData();
                }));
    }

    @Override
    public void getRecommendSongMenus(int curPage, int pageSize) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getRecommendSongMenus(curPage, pageSize)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(this::parseHomeData, throwable -> {
                    getView().showMessage(throwable);
                    handleLocalHomeData();
                }));
    }

    @Override
    public void getRecommendMvs(int curPage, int pageSize) {
        if (getView() == null) {
            return;
        }

        addSubscription(mModel.getRecommendMvs(curPage, pageSize)
                .compose(ResponseTransformer.handleResult())
                .compose(schedulerProvider.applySchedulers())
                .subscribe(this::parseHomeData, throwable -> {
                    getView().showMessage(throwable);
                    handleLocalHomeData();
                }));
    }

    @Override
    public void handleLocalHomeData() {
        HomeData homeData = CacheManager.getInstance().getHomeDataCacheForType(HOME);
        try {
            if (homeData != null) {
                HomeInfo homeInfo = JsonUtil.toObject(homeData.data, HomeInfo.class);
                parseHomeData(homeInfo);
            }
        } catch (Exception e) {
            LogUtils.e(e);
        }
    }

    @Override
    public void loadCacheDataForHome() {
        HomeData homeData = CacheManager.getInstance().getHomeDataCacheForType(HOME);
        try {
            if (homeData != null) {
                HomeInfo homeInfo = JsonUtil.toObject(homeData.data, HomeInfo.class);
                getView().setRecommend(homeInfo);
            }
        } catch (Exception e) {
            LogUtils.e(e);
        }
    }

    @Override
    public void parseHomeData(HomeInfo homeInfo) {
        try {
            HomeData homeData = new HomeData();
            homeData.data = JsonUtil.toString(homeInfo, false);
            homeData.type = HOME;
            CacheManager.getInstance().setHomeDataCacheForType(homeData, HOME);
            getView().setRecommend(homeInfo);
        } catch (Exception e) {
            LogUtils.e(e);
        }
    }
}
